﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neat;
using Neat.Mathematics;
using Sectors;
using SectorsDesigner.MapMaker;

namespace SectorsDesigner
{
    public static class ScriptHelper
    {
        public static string Script(SectorInfo s)
        {
            string script = "rem Sector Definition - " + s.Name + "\n";
            script += ":secdef_" + s.Name + "\n";
            script += "w_newsector " + s.Name;
            foreach (var vertex in s.Mesh.ToVertexList()) script += " " + GeometryHelper.Vector2String(vertex);
            script += "\n";
            script += "ws_floor " + s.FloorHeight.ToString() + "\n";
            script += "ws_ceiling " + s.CeilingHeight.ToString() + "\n";
            script += "ws_floortexture " + s.FloorTexture + "\n";
            script += "ws_ceilingtexture " + s.CeilingTexture + "\n";
            script += "ws_type " + ((int)s.SectorType).ToString() + "\n";
            script += "ws_damage " + s.DamagePoints.ToString() + "\n";
            script += "ws_bottom " + s.LowerSector + "\n";
            script += "ws_top " + s.UpperSector + "\n";
            script += "ws_gravity " + s.GravityRate + "\n";
            script += "ws_light " + s.LightIntensity + "\n";
            script += "ws_textureuv f " + GeometryHelper.Vector2String(s.FloorUV) + "\n";
            script += "ws_textureuv c " + GeometryHelper.Vector2String(s.CeilingUV) + "\n";
            script += "ws_create" + "\n";
            script += "\n";
            //TODO script += "ws_entered ; leaved ; idle

            return script;
        }

        public static string Script(Edge e)
        {
            string script = "rem Wall Definition - " + e.Wall.Name + "\n";
            var walls = e.ToWalls();
            foreach (var w in walls)
            {
                script += "w_newwall " + w.Name +
                    " " + GeometryHelper.Vector2String(w.StartPos) + " " + GeometryHelper.Vector2String(w.EndPos) + "\n";
                script += "ww_texture1 " + w.Texture + "\n";
                script += "ww_texture2 " + w.TextureB + "\n";
                script += "ww_uppertexture " + w.TextureU + "\n";
                script += "ww_lowertexture " + w.TextureL + "\n";
                script += "ww_textureuv f " + GeometryHelper.Vector2String(w.TextureUV) + "\n";
                script += "ww_textureuv b " + GeometryHelper.Vector2String(w.TextureBUV) + "\n";
                script += "ww_textureuv u " + GeometryHelper.Vector2String(w.TextureUUV) + "\n";
                script += "ww_textureuv l " + GeometryHelper.Vector2String(w.TextureLUV) + "\n";
                script += "ww_type " + ((int)w.WallType).ToString() + "\n";
                //script += "ww_create" + "\n";
                //TODO script += pushed, shot, passed
            }
            return script;
        }

        public static string Script(SectorsMapMakerControl w)
        {
            string script = "rem World Definition - " + w.Name+"\n";

            foreach (var ei in w.AliveEdges)
            {
                script += "\n" + Script(w.Edges[ei]);
            }

            foreach (var sector in w.Sectors)
            {
                script += "\n" + Script(sector);
            }

            script += "\n" + "w_createallwalls" + "\n";
            return script;
        }
    }
}
